<template>
  <el-dialog :title="`${isEdit ? '编辑' : '新增'}设备`" width="700px" :model-value="modelValue" @close="commEvent.onClose">
    <el-form ref="formRef" :model="formValue" :rules="rules">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="所属厂区" prop="factoryId">
            <el-select v-model="formValue.factoryId" :disabled="isEdit" placeholder="请选择所属厂区" filterable>
              <el-option v-for="(item, index) in factorys" :key="index" :label="item.label" :value="item.value || ''" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属产线" prop="lineId">
            <el-select v-model="formValue.lineId" :disabled="isEdit" placeholder="请选择所属产线" filterable>
              <el-option v-for="(item, index) in lines" :key="index" :label="item.label" :value="item.value || ''" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备编码" prop="code">
            <el-input v-model="formValue.code" placeholder="请输入设备编码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备名称" prop="name">
            <el-input v-model="formValue.name" placeholder="请输入设备名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备类型" prop="deviceType">
            <el-select v-model="formValue.deviceType" placeholder="请选择设备类型" filterable>
              <el-option v-for="(item, index) in devicetTypes" :key="index" :label="item.label" :value="item.value || ''" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="能耗等级" prop="energyGrade">
            <el-select v-model="formValue.energyGrade" placeholder="请选择能耗等级" filterable>
              <el-option v-for="(item, index) in energyGrades" :key="index" :label="item.label" :value="item.value || ''" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="额定功率" prop="ratedPower">
            <el-input v-model="formValue.ratedPower" placeholder="请输入额定功率">
              <template #append>kW</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="安装位置" prop="location">
            <el-input v-model="formValue.location" placeholder="请输入安装位置" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规格型号" prop="modelNumber">
            <el-input v-model="formValue.modelNumber" placeholder="请输入规格型号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产厂家" prop="manufacturer">
            <el-input v-model="formValue.manufacturer" placeholder="请输入生产厂家" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产日期" prop="productionDate">
            <el-date-picker
              v-model="formValue.productionDate"
              type="datetime"
              value-format="YYYY-MM-DD HH:mm:ss"
              placeholder="请选择生产日期"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="投运日期" prop="putrunDate">
            <el-date-picker
              v-model="formValue.putrunDate"
              type="datetime"
              value-format="YYYY-MM-DD HH:mm:ss"
              placeholder="请选择投运日期"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人" prop="personCharge">
            <el-input v-model="formValue.personCharge" placeholder="请输入负责人姓名" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="formValue.remark" type="textarea" :rows="3" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="commEvent.onClose">取消</el-button>
      <el-button type="primary" @click="commEvent.onEnter">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { computed, nextTick, PropType, ref, watch } from "vue"
import { ElMessage, FormInstance, FormRules } from "element-plus"
import { EnumProps } from "@/components/ProTable/interface"
import { addDevice, modifyDevice } from "@/api/modules/archive"
import { SystemSpace } from "@/api/interface/archive"
const props = defineProps({
  modelValue: Boolean,
  rows: { type: Object as PropType<SystemSpace.DevicePage>, default: () => {} },
  factorys: { type: Array as PropType<EnumProps[]>, default: () => [] },
  lines: { type: Array as PropType<EnumProps[]>, default: () => [] },
  devicetTypes: { type: Array as PropType<EnumProps[]>, default: () => [] },
  energyGrades: { type: Array as PropType<EnumProps[]>, default: () => [] }
})
const emit = defineEmits(["update:modelValue", "refresh"])
// 参数
const formRef = ref<FormInstance>() // 表单实例
const isEdit = computed(() => Boolean(props.rows?.id)) // 是否是编辑
// 表单数据
const formValue = ref({
  factoryId: undefined as undefined | number,
  lineId: undefined as undefined | number,
  code: "",
  name: "",
  deviceType: undefined as undefined | number,
  energyGrade: undefined as undefined | number,
  ratedPower: "",
  location: "",
  modelNumber: "",
  manufacturer: "",
  productionDate: "",
  putrunDate: "",
  personCharge: "",
  remark: ""
})
//  表单校验
const rules: FormRules = {
  factoryId: [{ required: true, message: "请选择所属厂区", trigger: "change" }],
  lineId: [{ required: true, message: "请选择所属产线", trigger: "change" }],
  code: [{ required: true, message: "请输入设备编码", trigger: "blur" }],
  name: [{ required: true, message: "请输入设备名称", trigger: "blur" }],
  deviceType: [{ required: true, message: "请选择设备类型", trigger: "change" }]
}
// 事件集合
const commEvent = {
  // 关闭弹窗
  onClose: () => {
    emit("update:modelValue", false)
  },
  // 确定
  onEnter: () => {
    if (!formRef.value) return
    formRef.value.validate(async valid => {
      if (!valid) return
      let res: any
      const { factoryId, lineId, deviceType, energyGrade } = formValue.value
      const params: SystemSpace.SaveDevice = {
        ...formValue.value,
        factoryId: Number(factoryId),
        lineId: Number(lineId),
        deviceType: Number(deviceType),
        energyGrade: Number(energyGrade)
      }
      if (props.rows?.id) {
        params.id = props.rows?.id
        delete params.factoryId
        delete params.lineId
        res = await modifyDevice(params)
      } else {
        res = await addDevice(params)
      }
      if (res?.code !== "200") return
      ElMessage.success("操作成功")
      commEvent.onClose()
      emit("refresh")
    })
  }
}
watch(
  () => props.modelValue,
  val => {
    if (val) {
      nextTick(() => {
        if (!formRef.value) return
        formRef.value.resetFields()
        if (props.rows?.id) {
          const {
            factoryId,
            lineId,
            code,
            name,
            deviceType,
            energyGrade,
            ratedPower,
            location,
            modelNumber,
            manufacturer,
            productionDate,
            putrunDate,
            personCharge,
            remark
          } = props.rows
          formValue.value = {
            factoryId,
            lineId,
            code,
            name,
            deviceType,
            energyGrade,
            ratedPower,
            location,
            modelNumber,
            manufacturer,
            productionDate,
            putrunDate,
            personCharge,
            remark
          }
        }
      })
    }
  }
)
</script>

<style lang="scss" scoped></style>
